﻿package com.fecesaids.greatgame.utils {
	import flash.display.Sprite;
	import flash.media.Sound;  
	import flash.net.URLRequest;
	import flash.media.SoundLoaderContext;
	import flash.events.Event;
	import flash.media.SoundChannel;
	import flash.media.ID3Info;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;
	public class AudioStream extends Sprite{
		private var _audioList:Array;
		private var _soundList:Array;
		private var _id3List:Array = [];
		private var _curTrack:int = 0;
		private var _context:SoundLoaderContext;
		private var _channel:SoundChannel;
		private var _loadedPos:int = -1;
		private var _mouseDown:Boolean = false;
		private var _mouseClickOrigin:Number;
		private var _volPercent:Number = .5;
		private var _lastVolPercent:Number = .5;
		private var _isDown = true;
		public function AudioStream(...audioList:Array):void{
			loadAudioList(audioList);
			_context = new SoundLoaderContext(8000, true);
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			this.addEventListener(MouseEvent.MOUSE_DOWN, myMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, myMouseUp);
			this.addEventListener(MouseEvent.MOUSE_MOVE, myMouseMove); 
			this.addEventListener(MouseEvent.ROLL_OUT, myMouseOut); 
			this.AudStr_VolumeKnob_MC.x = 56.85;
			this.AudStr_VolumeKnob_MC.y = 19.6;
			AudStr_SoundLines_MC.visible = true;
		}
		public function getWidth():Number{
			return AudStr_Background_MC.width;
		}
		public function getHeight():Number{
			return AudStr_Background_MC.height;
		}
		private function onEnterFrame(event:Event):void
		{
			this.songLabel.scrollH++;
			if(this.songLabel.scrollH >= this.songLabel.maxScrollH){
				this.songLabel.scrollH = 0;
			}
			if(_channel != null){
				if(_lastVolPercent != _volPercent){
					var transform:SoundTransform = _channel.soundTransform;
					transform.volume = _volPercent;
					_channel.soundTransform = transform;
					_lastVolPercent = _volPercent;
				}
			}
			if(_isDown){
				if(this.y < stage.stageHeight){
					this.y += 1;
					if(this.y >= stage.stageHeight){
						this.y = stage.stageHeight;
						this.AudStr_MinButton_BT.visible = false;
					}
				}
				if(this.AudStr_MaxButton_BT.y > -13.9){
					this.AudStr_MaxButton_BT.y -= 1;
					if(this.AudStr_MaxButton_BT.y <= -13.9){
						this.AudStr_MaxButton_BT.y = -13.9;
					}
				}
			}
			else{
				if(this.y > stage.stageHeight - getHeight()){
					this.y -= 1;
					if(this.y <= stage.stageHeight - getHeight()){
						this.y = stage.stageHeight - getHeight();
						this.AudStr_MaxButton_BT.visible = false;
					}
				}
				if(this.AudStr_MaxButton_BT.y < 0){
					this.AudStr_MaxButton_BT.y += 1;
					if(this.AudStr_MaxButton_BT.y >= 0){
						this.AudStr_MaxButton_BT.y = 0;
					}
				}
			}
		}
		public function loadAudioList(audioList:Array):void{
			if(audioList.length > 0){
				_audioList = randomizeArray(audioList);
				var i:int;
				var numSounds:int = _audioList.length;
				_soundList = [];
				_loadedPos = -1;
				for(i = 0; i < numSounds; i++){
					_soundList.push(new Sound());
					_id3List.push("                      " + _audioList[i][0][1] + "                      ");
				}
				streamNextSound();
			}
		}
		private function streamNextSound():void{
			var transform:SoundTransform = new SoundTransform(_volPercent);
			if(_loadedPos >= _curTrack){
				_channel = _soundList[_curTrack].play(0, 0, transform);
				this.songLabel.text = _id3List[_curTrack];
				_channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			}
			else{
				var req:URLRequest = new URLRequest(_audioList[_curTrack][0][0]);
				this.songLabel.text = _id3List[_curTrack];
				_soundList[_curTrack].load(req, _context);
				_channel = _soundList[_curTrack].play(0, 0, transform);
				_soundList[_curTrack].addEventListener(Event.COMPLETE, completeHandler);
				_channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			}
		}
		private function completeHandler(evt:Event):void{
			_soundList[_curTrack].removeEventListener(Event.COMPLETE, completeHandler);
			if(_loadedPos + 1 < _soundList.length)
				_loadedPos++;
		}
		private function soundCompleteHandler(evt:Event):void{
			_channel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			_curTrack++;
			if(_curTrack >= _soundList.length){
				_curTrack = 0;
			}
			streamNextSound();
		}
		private function randomizeArray(array:Array):Array{
			var newArray:Array = new Array();
			while(array.length > 0){
				newArray.push(array.splice(Math.floor(Math.random()*array.length), 1));
			}
			return newArray;
		}
		private function myMouseDown(eventObject:MouseEvent) {
			if(eventObject.target.name == "AudStr_VolumeKnob_BT"){
				_mouseDown = true;
				_mouseClickOrigin = this.AudStr_VolumeKnob_MC.mouseX;
			}
			else if(eventObject.target.name == "AudStr_MaxButton_BT"){
				if(_isDown){
					_isDown = false;
					this.AudStr_MinButton_BT.visible = true;
				}
			}
			else if(eventObject.target.name == "AudStr_MinButton_BT"){
				if(!_isDown){
					_isDown = true;
					this.AudStr_MaxButton_BT.visible = true;
				}
			}
		}
		private function myMouseUp(eventObject:MouseEvent) {
			_mouseDown = false;
		}
		private function myMouseMove(eventObject:MouseEvent) {
			if(_mouseDown){
				this.AudStr_VolumeKnob_MC.x = mouseX - _mouseClickOrigin;
				this.AudStr_VolumeKnob_MC.y = 19.6;
				if(this.AudStr_VolumeKnob_MC.x < 20){
					this.AudStr_VolumeKnob_MC.x = 20;
				}
				else if(this.AudStr_VolumeKnob_MC.x > 93.7){
					this.AudStr_VolumeKnob_MC.x = 93.7;
				}
				var pos:Number = this.AudStr_VolumeKnob_MC.x - 20;
				_volPercent = pos / 73.7;
			}
		}
		private function myMouseOut(eventObject:MouseEvent){
			_mouseDown = false;
		}
	}
}